#include "Materiau.h"
#include <stdio.h>

Materiau::Materiau(int type, int i) {
	typeMat = type;

	if (typeMat == BOIS) {
		coul1 = Couleur(0.75,0.44,0.04);
		coul2 = Couleur(0.9,0.6,0.2);
	}
	else if (typeMat == MARBRE) {
		coul1 = Couleur(0.89,0.89,0.89);
		coul2 = Couleur(0.96,0.6,0.51);
	}
	intensite = i;

}

void Materiau::setTypeMat(int type) {
	typeMat = type;

	if (typeMat == BOIS) {
		coul1 = Couleur(0.75,0.44,0.04);
		coul2 = Couleur(0.9,0.6,0.2);
	}
	else if (typeMat == MARBRE) {
		coul1 = Couleur(0.89,0.89,0.89);
		coul2 = Couleur(0.96,0.6,0.51);
	}
}

Couleur Materiau::appliquerMateriau(const Point &intersect) {
	if (typeMat == BOIS) {
		double t = intensite * myPerlin.noise(intersect.getX(), intersect.getY(), intersect.getZ());
		double n = abs(t - (int)t);

		/* Interpolation sinusoidale */
		double ft = n * M_PI;
		double f = (1 - cos(ft)) * 0.5;

		return ((coul1*(1 - f)) + (coul2 * f));
	}
	else if (typeMat == MARBRE) {
		double noiseCoef = 0.0;
		for (int level=1; level < 10; level++) {
			noiseCoef += (1.0 / level) * fabs(myPerlin.noise(level * intensite * intersect.getX(),
															 level * intensite * intersect.getY(),
															 level * intensite * intersect.getZ()));
		}

		/* Interpolation sinusoidale */
		noiseCoef = 0.5 * sin( (intersect.getX() + intersect.getY()) * intensite + noiseCoef) + 0.5;
		return ((coul1 * noiseCoef) + (coul2 * (1.0 - noiseCoef)));

	}
	return Couleur();
}